﻿using System;
using System.Windows;
using System.Windows.Media.Imaging;
using eLearning.Model;
using eLearning.Util;
using eLearning.View;
using Microsoft.Phone.Controls;

namespace eLearning
{
    public partial class Home : PhoneApplicationPage
    {
        public Home()
        {
            InitializeComponent();
        }
        
        protected override void OnNavigatedTo(System.Windows.Navigation.NavigationEventArgs e)
        {
            CheckLoggedUser();
        }
        
        protected override void OnNavigatedFrom(System.Windows.Navigation.NavigationEventArgs e)
        {
            base.OnNavigatedTo(e);
            if (e.NavigationMode == System.Windows.Navigation.NavigationMode.Back)
            {
                App.Quit();
            }
        }

        private void StackPanel_Tap(object sender, System.Windows.Input.GestureEventArgs e)
        {
            if (textBlockUser.Text.Equals("Login"))
            {
                Utility.GotoPage(this, "/View/LoginPage.xaml");
            }
            else
            {
                if (MessageBox.Show("Logout now?", "Confirmation", MessageBoxButton.OKCancel) == MessageBoxResult.OK)
                {
                    SavedUser.Instance.DestroyUser();
                    CheckLoggedUser();
                }
            }
        }

        private void tileMahasiswa_Tap(object sender, System.Windows.Input.GestureEventArgs e)
        {
            LoginPage.userType = User.UserType.Mahasiswa;
            
            if (SavedUser.Instance.User == null)
                Utility.GotoPage(this, "/View/LoginPage.xaml");
            else if (SavedUser.Instance.User.Type == User.UserType.Mahasiswa)
                Utility.GotoPage(this, "/View/Mahasiswa/DashboardPage.xaml");
            else
                MessageBox.Show("Please Login as Mahasiswa!");
        }

        private void tileDosen_Tap(object sender, System.Windows.Input.GestureEventArgs e)
        {
            LoginPage.userType = User.UserType.Dosen;
            
            if (SavedUser.Instance.User == null)
                Utility.GotoPage(this, "/View/LoginPage.xaml");
            else if (SavedUser.Instance.User.Type == User.UserType.Dosen)
                Utility.GotoPage(this, "/View/Dosen/DashboardPage.xaml");
            else
                MessageBox.Show("Please Login as Dosen!");
        }

        //mengecek cache login user
        private void CheckLoggedUser()
        {
            if (SavedUser.Instance.User != null)
            {
                textBlockUser.Text = "Logout " + SavedUser.Instance.User.ToString();
                imageLogin.Source = new BitmapImage(new Uri("Images/appbar.smile.1.rest.png", UriKind.RelativeOrAbsolute));
            }
            else
            {
                textBlockUser.Text = "Login";
                imageLogin.Source = new BitmapImage(new Uri("Images/appbar.key.png", UriKind.RelativeOrAbsolute));
            }
        }

        private void tileThread_Tap(object sender, System.Windows.Input.GestureEventArgs e)
        {
            Utility.GotoPage(this, "/View/ThreadPage.xaml");
        }
        
        private void tileWiki_Tap(object sender, System.Windows.Input.GestureEventArgs e)
        {
            Utility.GotoPage(this, "/View/WikiPage.xaml");
        }

        private void tileInfo_Tap(object sender, System.Windows.Input.GestureEventArgs e)
        {
            Utility.GotoPage(this, "/View/PengumumanPage.xaml");
        }
        
        private void imageHelp_Tap(object sender, System.Windows.Input.GestureEventArgs e)
        {
            Utility.GotoPage(this, "/View/AboutPage.xaml");
        }

    }
}